import os
import xml.etree.ElementTree as ET
from PIL import Image, ImageDraw

def extract_center_points(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()

    center_points = []

    for obj in root.findall('object'):
        bbox = obj.find('bndbox')
        xmin = float(bbox.find('xmin').text)
        ymin = float(bbox.find('ymin').text)
        xmax = float(bbox.find('xmax').text)
        ymax = float(bbox.find('ymax').text)
        center_x = int((xmin + xmax) / 2)
        center_y = int((ymin + ymax) / 2)
        center_points.append((center_x, center_y))

    return center_points

def draw_center_points(image_size, center_points):
    image = Image.new('RGB', image_size, (255, 255, 255))
    draw = ImageDraw.Draw(image)

    for center_x, center_y in center_points:
        draw.ellipse((center_x - 1, center_y - 1, center_x + 1, center_y + 1), fill=(0, 0, 0))

    return image

def main():
    xml_folder = 'xml_all_20231011'  # 文件夹路径，包含所有.xml文件
    image_size = (960, 540)  # 底图大小

    center_points = []

    for xml_file in os.listdir(xml_folder):
        if xml_file.endswith('.xml'):
            xml_path = os.path.join(xml_folder, xml_file)
            center_points.extend(extract_center_points(xml_path))

    result_image = draw_center_points(image_size, center_points)
    result_image.save('xml_all_20231011.png')

if __name__ == '__main__':
    main()
